package com.haohe.wx.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.haohe.wx.dto.UserLeaveDto;
import com.haohe.wx.services.SubscribeService;
import com.haohe.wx.util.WXUtil;
import com.haohe.wx.wechat.common.WXBizMsgCrypt;



/** 微信与应用的入口 */
@Controller
@RequestMapping(value="/wxCoreController")
public class WxCoreController extends BaseController {

	private Logger logger = Logger.getLogger(WxCoreController.class);
	
	private WXBizMsgCrypt pc;
	
	@Autowired
	private SubscribeService subscribeService;
	
	

	/** 如果5秒内没有回复企业号，企业号再次调用程序 */
	@RequestMapping(value="/show")
	public void show(String msg_signature, String timestamp, String nonce,
			String echostr, HttpServletResponse response, HttpServletRequest request){
	
		try {
			
			String method = request.getMethod();
			
			response.setContentType("text/html; charset=utf-8");
			
			PrintWriter printWriter = response.getWriter();

			pc = new WXBizMsgCrypt();

			if(StringUtils.isNotBlank(method) && "GET".equals(method)){
				String msg = pc.verifyUrl(msg_signature, timestamp, nonce, echostr);
				if(StringUtils.isBlank(msg)){
					logger.info("校验失败！失败原因："+msg);
					return;
				}
				printWriter.write(msg);
			}else{
				String receiveMessage = WXUtil.parseStream2Str(request.getInputStream());
				if(StringUtils.isBlank(receiveMessage)){
					printWriter.flush();
					printWriter.close();
					return;
				}				
				/**校验msg_signature的正确性，对encrypt进行解密*/
				String msg = pc.decryptMsg(msg_signature, timestamp, nonce, receiveMessage);
				
				String returnMsg = subscribeService.subscribe(timestamp, nonce, msg);
				
			    System.out.println("微信获取字符串=="+msg);
			    
				printWriter.write(returnMsg);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/** 跳转请假注意事项页 */
	@RequestMapping(value="/leaveView")
	public ModelAndView leaveView(){
		Map<String,Object> map = new HashMap<>();
		//map.put(arg0, arg1);
		return new ModelAndView("/company/qingjiadanNote",map);
	}
	/** 跳转请假填写页 */
	@RequestMapping(value="/writeView")
	public ModelAndView writeView(){
		Map<String,Object> map = new HashMap<>();
		//map.put(arg0, arg1);
		return new ModelAndView("/company/qingjiadan",map);
	}
	/** 跳转加班填写页 */
	@RequestMapping(value="/workView")
	public ModelAndView workView(){
		Map<String,Object> map = new HashMap<>();
		//map.put(arg0, arg1);
		System.out.println("---");
		return new ModelAndView("/company/jiabandan",map);
	}
	/** 跳转补签填写页 */
	@RequestMapping(value="/buqianView")
	public ModelAndView buqianView(){
		Map<String,Object> map = new HashMap<>();
		//map.put(arg0, arg1);
		return new ModelAndView("/company/buqiandan",map);
	}
	
	/** 请假信息存库 */
	@RequestMapping(value="/leaveInsert")
	public ModelAndView leaveInsert(UserLeaveDto userLeaveDto){
		ModelAndView view = new ModelAndView();
		//TEmployeeLeave tEmployeeLeave = new TEmployeeLeave();
		try{
//			tEmployeeLeave.setDepart(userLeaveDto.getDepart());
//			tEmployeeLeave.setName(userLeaveDto.getName());
//			tEmployeeLeave.setJob(userLeaveDto.getJob());
//			tEmployeeLeave.setLeaveType(userLeaveDto.getLeave_type());
//			tEmployeeLeave.setLeaveCause(userLeaveDto.getLeave_cause());
//			tEmployeeLeave.setLeaveDate(userLeaveDto.getLeave_date());
//			tEmployeeLeave.setWorkApply(userLeaveDto.getWork_apply());
//			tEmployeeLeave.setPhone(userLeaveDto.getPhone());
//			tEmployeeLeave.setContent(userLeaveDto.getContent());
//			tEmployeeLeave.setLeader(userLeaveDto.getLeader());
			//tEmployeeLeaveService.insert(tEmployeeLeave);
			logger.info("插员工请假表成功。");
			
			view.addObject("userLeaveDto", userLeaveDto);
			view.setViewName("/company/qingjiaSure");
			
		}catch(Exception t){
			t.printStackTrace();
			logger.error(t.getMessage());
		}finally{
			try{
				
			}catch(Exception e){
				logger.error("插员工请假表失败。");
			}
		}
		return view;
	}
}
